#!/usr/bin/groovy
import com.cloudbees.plugins.credentials.*

@Library('test-shared-library') _

def withRequiredCredentials(commons, groovy.lang.Closure code) {
    commons.withAWSCredentials {
        commons.withJenkinsCredentials {
            commons.withGitPullCredentials {
                commons.withDAICredentials {
                    commons.withGitPushCredentials {
                        commons.withSigningCredentials {
                            commons.withGitPrivateKey {
                                commons.withAWSPrivateKey {
                                    commons.withCondaCredentials {
                                        commons.withPipyCredentials {
                                            commons.withDockerHubCredentials {
                                                commons.withNexusCredentials {
                                                    commons.withDatabricksCredentials {
                                                        code()
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

node("mr-0xc10") {
    cleanWs()
    checkout scm
    def commons = load 'ci/commons.groovy'

    stage("Deploy Testing Infrastructure on AWS") {
        withRequiredCredentials(commons) {
            def ami = dir("ci/aws/packer") {
                return commons.withPacker {
                    return commons.packerBuild()
                }
            }
            commons.withTerraform {
                dir("ci/aws/terraform") {
                    def publicHostname = "sparkling-jenkins.oss.h2o.ai"

                    def initJenkinsFile = "modules/jenkins/scripts/init_jenkins.groovy"
                    writeFile file: initJenkinsFile, text: readFile(initJenkinsFile)
                            .replaceAll("SUBST_GITHUB_PULL_USER", env.GITHUB_PULL_USER)
                            .replaceAll("SUBST_GITHUB_PULL_PASS", env.GITHUB_PULL_PASS)
                            .replaceAll("SUBST_GITHUB_TOKEN", env.GITHUB_TOKEN)
                            .replaceAll("SUBST_SIGN_KEY", env.SIGN_KEY)
                            .replaceAll("SUBST_DAI_LICENSE_KEY", env.DRIVERLESS_AI_LICENSE_KEY)
                            .replaceAll("SUBST_AWS_ACCESS_KEY_ID", env.AWS_ACCESS_KEY_ID)
                            .replaceAll("SUBST_AWS_SECRET_ACCESS_KEY", env.AWS_SECRET_ACCESS_KEY)
                            .replaceAll("SUBST_GITHUB_SSH_USER", env.GITHUB_SSH_USER)
                            .replaceAll("SUBST_PUBLIC_HOSTNAME", publicHostname)
                            .replaceAll("SUBST_JENKINS_USER", env.SW_JENKINS_USER)
                            .replaceAll("SUBST_JENKINS_PASS", env.SW_JENKINS_PASS)
                            .replaceAll("SUBST_AMI_ID", ami)
                            .replaceAll("SUBST_NEXUS_USERNAME", env.NEXUS_USERNAME)
                            .replaceAll("SUBST_NEXUS_PASSWORD", env.NEXUS_PASSWORD)
                            .replaceAll("SUBST_PIPY_USERNAME", env.PIPY_USERNAME)
                            .replaceAll("SUBST_PIPY_PASSWORD", env.PIPY_PASSWORD)
                            .replaceAll("SUBST_ANACONDA_USERNAME", env.ANACONDA_USERNAME)
                            .replaceAll("SUBST_ANACONDA_PASSWORD", env.ANACONDA_PASSWORD)
                            .replaceAll("SUBST_DOCKERHUB_USERNAME", env.DOCKERHUB_USERNAME)
                            .replaceAll("SUBST_DOCKERHUB_PASSWORD", env.DOCKERHUB_PASSWORD)
                            .replaceAll("SUBST_DATABRICKS_HOST", env.DATABRICKS_HOST)
                            .replaceAll("SUBST_DATABRICKS_TOKEN", env.DATABRICKS_TOKEN)

                    def initSSLFile = "modules/jenkins/scripts/init-ssl.sh"
                    writeFile file: initSSLFile, text: readFile(initSSLFile)
                            .replaceAll("SUBST_PUBLIC_HOSTNAME", publicHostname)

                    def extraVars = [
                            "-var signing_file=${env.RING_FILE_PATH}",
                            "-var public_hostname=${publicHostname}",
                            "-var github_key_file=${env.GITHUB_SSH_KEY}",
                            "-var aws_key_file=${env.AWS_SSH_KEY}"
                    ]
                    commons.terraformImport(ami)
                    commons.terraformApply(extraVars.join(" "))
                    def values = commons.extractTerraformOutputs(["docker_registry_id", "jenkins_url"])
                    def valuesAsString = values.collect { "${it.key}=${it.value}" }.join("\n") + "\n"
                    writeFile file: "infra.properties", text: valuesAsString
                }
            }
        }
    }

    stage("Save Infrastructure State") {
        commons.withGitPushCredentials {
            commons.gitCommit(["ci/aws/terraform/terraform.tfstate", "ci/aws/terraform/infra.properties"], "Saving Infrastructure State")
        }
    }
}

stage("Build and publish docker image") {
    build job: 'TEST_INFRA_BUILD_DOCKER_IMAGE/master'
}
